import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.*;

class MyClassTest {


    MyClass myclass;

    @BeforeEach
    public void setUp(){
        this.myclass=new MyClass();
    }

    @AfterEach
    public  void  tearDown(){
        this.myclass=null;
    }

    @Test
    public void test(){
        assertThrows(IllegalArgumentException.class, () -> {
            myclass.init("李四",-1);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            myclass.init("王五",-2);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            myclass.init("赵六",100);
        });
        assertThrows(IllegalArgumentException.class, () -> {
            myclass.init("zs",20);
        });
    }

    public static void main(String[] args) throws Exception {


        // 新建MyClass
        MyClass myclass= new MyClass();
        // 获取MyClass的Class实例
        Class<MyClass> c = MyClass.class;
        // 获取 init() 方法的Method实例
        Method minit = c.getMethod("init", new Class[]{String.class, int.class});
        // 执行该方法
        minit.invoke(myclass, new Object[]{"张三", 18});
        iteratorAnnotations(minit);

    }


    public static void iteratorAnnotations(Method method) {

        // 判断 init() 方法是否包含InitMethod注解
        if (method.isAnnotationPresent(InitMethod.class)) {
            // 获取该方法的InitMethod注解实例
            InitMethod initmethod = method.getAnnotation(InitMethod.class);
            // 获取InitMethod的值，并打印出来
            String[] values = initmethod.value();
            for (String str : values)
                System.out.printf(str + ", ");
            System.out.println();
        }

        // 获取方法上的所有注解，并打印出来
        Annotation[] annotations = method.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
    }
}